var uid = getParameter("uid");
var day = getParameter("Day");
var date  = getParameter("date");
var now = new Date(date);
var arg;
$(function() {
	charts(now);
});
function charts(now) {
	var befdate;
	var aftdate;
	if (day == 30) {
		arg = 30;
		befdate = getLastDay(now, 29).format("yyyy-MM-dd");
	    aftdate = getNextDay(now, 0).format("yyyy-MM-dd");
	} else if (day == 7) {
		arg = 7;
		befdate = getLastDay(now, 6).format("yyyy-MM-dd");
	    aftdate = getNextDay(now, 0).format("yyyy-MM-dd");
	}else if(day == 1){
		arg = 1;
		befdate = now.format("yyyy-MM-dd");
	    aftdate = now.format("yyyy-MM-dd");
	}
	var urlStr = serverPath + "/app/doctor/diabetes/sugarView.do";  
	var jsonStr = 
	    '{'+
	    '	   "requestid":"",'+
	    '	   "requestsource":"diabetes",'+
	    '	   "sessionid":"",'+
	    '	   "uid":"'+uid+'",'+
	    '	   "method":"showSugarTable",'+
	    '	   "version":"1.0",'+
	    '	   "requestlist":'+
	    '	   ['+
	    '	      {'+
	    '			 "date":"'+now.format("yyyy-MM-dd")+'",'+
	    '			 "befdate":"'+befdate+'",'+
	    '			 "aftdate":"'+aftdate+'",'+
	    '			 "day":"'+day+'",'+
	    '	      }'+
	    '	   ]'+
	    '	}';  
	  
	  UseAjax(urlStr, jsonStr, document.cookie, rt, '1');
}

function rt(data) {
	var records =  data.resultlist[0].records;
	var html="";
	html = '<table class="bordered"><thead> '+ 
	 "<tr><th rowspan='2'>日期</th><th colspan='2'>早餐</th><th colspan='2'>午餐</th><th colspan='2'>晚餐</th><th>睡前</th></tr><tr><th><font color='white'>前</font></th><th><font color='white'>后</font></th><th><font color='white'>前</font></th><th><font color='white'>后</font></th><th><font color='white'>前</font></th><th><font color='white'>后</font></th><th></th>"+ 
     '</tr></thead>';
	if(records.length > 0){
	for (var int = 0; int < records.length; int++) {
		var wi = records[int];//.substring(0,10)
		html+= '<tr><td>'+wi.dates.substring(5,10)+
	     '</td>'+canqian(isnull(wi.breakfast_pre))
	     +canhou(isnull(wi.breakfast_aft))+canqian(isnull(wi.lunch_pre))
	     +canhou(isnull(wi.lunch_aft))
	     +canqian(isnull(wi.dinner_pre))+canhou(isnull(wi.dinner_aft))
	     +canhou(isnull(wi.sleep_pre))+'</tr>';
	}
	}
	$("#container").html(html);
	//设置表格颜色
	$("tr:odd").each(function(index) {
		$(this).css("background-color","#e0ecfb");
	});
	$("tr:first").css("color","#ffffff");
}
//判断是否合理
function canqian(r){
	if(r >= 4.3001 && r <= 7.0001){
		return '<td>'+r+'</td>';
	}else{
		return '<td style="color:red;">'+r+'</td>';
	}
}
function canhou(r){
	if(r>4.3001 && r < 10.0001){
		return '<td>'+r+'</td>';
	}else{
		return '<td style="color:red;">'+r+'</td>';
	}
}
function isnull(value) {
	if(value == null || value == "" || value == undefined){
		return "";
	}else{
		return value;
	}
}
$(function() {
	// swipe 的dome
	$("#container").swipe(
			{
				swipeLeft : function(event, direction, distance, duration,
						fingerCount) {

					now = getNextDay(now, arg);
					charts(now);
				},
				swipeRight : function(event, direction, distance, duration,
						fingerCount) {
					now = getLastDay(now, arg);
					charts(now);
				}
			});

});
